let express = require('express');
let router = express.Router();
let URL = require('url');
let mysql = require('mysql');
let bodyParser = require('body-parser');
let cookieParser = require('cookie-parser');
let session = require('cookie-session');
let mysqlhelp = require("../common/sql");
router.use(bodyParser.urlencoded({extended: true}));
// var User = require('./user'); // get

/**
 * 查询所有的文章
 */
router.post('/getArticeData', function (req, res, next) {
    let sql = ''; // 执行的sql
    let parameter = [req.body.id]; // 请求的参数
    if (req.body.id == 0 || req.body.id == undefined) {
        sql = 'SELECT * FROM aritice_index';
    } else {
        sql = 'SELECT * FROM aritice_index WHERE id = ? ';
    }
    mysqlhelp.select(sql, parameter).then(function (data) {
        data.ariticeTotal = data.result.length;
        data.isEffective = true;
        res.send(data)
    }).catch(function (err) {
        console.log(err)
    })
});

/**
 * 首页分页查询文章数据
 */
router.post('/getIndexArticeData', function (req, res, next) {
    let sql = ''; // 执行的sql
    let parameter = [Number((req.body.page - 1) * req.body.maxCount), Number(req.body.maxCount)]; // 请求的参数
    sql = 'select sql_calc_found_rows * from aritice_index limit ?, ?';
    mysqlhelp.select(sql, parameter).then(function (data) {
        data.ariticeTotal = data.result.length;
        data.isEffective = true;
        res.send(data)
    }).catch(function (err) {
        console.log(err)
    })
});


/**
 * 删除文章
 * @type {Router|router|*}
 */
router.post('/deleteAritice', function (req, res, next) {
    let sql = 'delete from aritice_index where id = ? ';
    let parameter = req.body.id;
    mysqlhelp.select(sql, parameter).then(function (data) {
        data.isEffective = true;
        res.send(data);
    }).catch(function (err) {
        console.log(err)
    })
});
module.exports = router;
